package org.example.dao.test;

import org.apache.commons.collections4.CollectionUtils;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yuntao.shen
 * @date 2018/12/19
 */
@SpringBootTest
public class CustomMybatisGeneratorRun {
    @Test
    public void main() throws Exception {
        CustomMybatisGeneratorRun app = new CustomMybatisGeneratorRun();
        app.generator();
    }

    private void generator() throws Exception {

        List<String> warnings = new ArrayList<>();
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("mybatis-generatorDb1.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(resourceAsStream);
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);

        if (CollectionUtils.isNotEmpty(warnings)) {
            for (String warning : warnings) {
                System.out.println(warning);
            }
        }
    }
}
